package common.post.service;

import java.util.Date;
import java.util.List;

import common.customer.bean.Customer;
import common.post.bean.Post;
import common.post.bean.PostComment;
import common.post.dao.PostCommentDao;
import common.post.dao.PostDao;

public class PostCommentServiceImpl implements PostCommentService {
	private PostCommentDao postCommentDao;
	private PostDao postDao;
	@Override
	public boolean addCommentReply(long commentId, String replyContent,
			Customer customer) {
		PostComment parent = this.postCommentDao.get(commentId);
		
		PostComment pc = new PostComment();
		pc.setContent(replyContent);
		pc.setCustomer(customer);
		pc.setParent(parent);
		pc.setPublishTime(new Date());
		long id = this.postCommentDao.save(pc);
		if(id>0){
			return true;
		}else{
			return false;
		}
	}
	@Override
	public boolean addPostComment(long postId,String replyContent,Customer customer) {
		Post post = this.postDao.get(postId);
		
		post.setReply((post.getReply()+1));
		this.postDao.update(post);
		
		PostComment pc = new PostComment();
		pc.setContent(replyContent);
		pc.setCustomer(customer);
		pc.setPost(post);
		pc.setPublishTime(new Date());
		
		
		long id = this.postCommentDao.save(pc);
		if(id>0){
			return true;
		}else{
			return false;
		}
	}
	@Override
	public long getPostCommentCountBYPostId(long postId) {
		String queryStr = "select count(p) from common.post.bean.PostComment p where p.post.id="+postId;
		return (Long)this.postCommentDao.find(queryStr,true).get(0);
	}

	@Override
	public List<PostComment> getPostCommentsBYPostId(long postId, int page,
			int pageSize) {
		String queryStr = "from common.post.bean.PostComment p where p.post.id="+postId;
		return this.postCommentDao.find(queryStr, page, pageSize);
	}
	//=============getter and setter methods======================
	public PostCommentDao getPostCommentDao() {
		return postCommentDao;
	}
	public void setPostCommentDao(PostCommentDao postCommentDao) {
		this.postCommentDao = postCommentDao;
	}
	public PostDao getPostDao() {
		return postDao;
	}
	public void setPostDao(PostDao postDao) {
		this.postDao = postDao;
	}
}
